package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceParam;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询围栏信息业务:geofenceQuery={}", geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.selectGeofence(geofenceQuery);
        //之前没有对围栏的车辆数据进行处理,现在需要进行业务补充:
        /*遍历围栏集合,依次给每一个围栏补充车辆数据*/
        //1.遍历围栏集合
        for (int i = 0; i < list.size(); i++) {
            //2.依次取出每个围栏对象
            GeofenceVO geofenceVO = list.get(i);
            //3.封装车辆查询对象,查询条件:围栏id
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            //4.调用车辆数据层,直接根据条件查询当前围栏下的所有的车辆
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
            //5.获取当前围栏绑定车辆总数
            int totalNum = vehicleVOList.size();
            //6.定义变量保存可用车辆数
            int availableNum = 0;
            //7.依次遍历每一辆车,如果车辆状态为1(空闲),可用车辆数+1
            for(VehicleVO vehicleVO : vehicleVOList){
                if(vehicleVO.getStatus().equals("1")){
                    availableNum++;
                }
            }
            if(availableNum>0){
                geofenceVO.setAvailableNum(availableNum);
            }else{
                geofenceVO.setAvailableNum(0);
            }
            geofenceVO.setTotalNum(totalNum);
            //因为后续要查看当前围栏上所有车辆的详细信息,所以把查出来的车辆列表也赋值给围栏
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return list;
    }

    @Override
    public void saveGeofence(GeofenceParam geofenceParam) {
        log.debug("保存围栏信息业务:geofenceParam={}", geofenceParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam, geofence);
        if (geofence.getId() != null) {
            geofence.setUpdateTime(new Date());
            geofenceMapper.update(geofence);
        } else {
            geofence.setStatus("1");//启用状态
            geofence.setCreateTime(new Date());
            geofenceMapper.insert(geofence);
        }
    }

    @Override
    public void deleteGeofence(Long id) {
        log.debug("删除围栏信息业务:id={}", id);
        //业务:需要判断该围栏是否有绑定车辆,如果有绑定的车辆,就不能删除!
        //1.封装车辆查询的DTO层对象
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        //2.调用车辆查询接口查询该围栏下的车辆列表
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        //3.如果该围栏下有车辆,业务层需要抛出操作失败的异常
        if (list.size() > 0 && list != null) {
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        } else {
            //4.如果该围栏下没有车辆,可以正常删除
            geofenceMapper.deleteById(id);
        }
    }
}
